package com.daiyang.lottery.controller.handler;

import com.daiyang.lottery.common.errorcode.GlobalErrorCodeConstants;
import com.daiyang.lottery.common.exception.ControllerException;
import com.daiyang.lottery.common.exception.ServiceException;
import com.daiyang.lottery.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
//加上这个注解在这个方法里面就能捕获全局异常
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exceptionHandler(Exception e) {
        log.info("服务器内部错误：",e);

        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode()
                ,e.getMessage());
    }


    @ExceptionHandler(value =ControllerException.class)
    public CommonResult<?> controllerExceptionHandler(ControllerException e) {
        log.info("serviceException:",e);

        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode()
                ,e.getMessage());
    }

    @ExceptionHandler(value =ServiceException.class)
    public CommonResult<?> serviceExceptionHandler(ServiceException e) {
        log.info("serviceException:",e);

        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode()
                ,e.getMessage());
    }
}
